// pages/productDetail/productDetail.js

var app = getApp()
Page({
  data: {
    current: 0,
    cartList: [],
  },
  onLoad: function (options) {
    getGroupDetail(this, options.id, options.agentId)

  },
  swichNav: function (e) {//点击切换样式
    var that = this;
    if (this.data.current === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        current: e.target.dataset.current
      })
    }
  },
  onReady: function (options) {
    // 页面渲染完成    
  },
  onShow: function () {
    // 页面显示
    this.setData({
      cartList: []
    })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
    selectInfo:function(){
    var that = this;
    wx.navigateTo({
      url: '../groupInfo/groupInfo?otherId=' + that.data.getGroupDetails.otherId,
    })
  },
    calling: function () {
       var that = this;
       //   console.log(that.data.Object.phone);
       wx.makePhoneCall({
          phoneNumber: that.data.getGroupDetails.phone, //此号码并非真实电话号码，仅用于测试
          success: function () {
             console.log("拨打电话成功！")
          },
          fail: function () {
             console.log("拨打电话失败！")
          }
       })
    }
  ,
  sendMsgToBuy: function (e) {//获取用户所选中产品的详细信息
    var that = this;
    console.log(that.data.cartList.length)
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      if (!config) {
        getCartInfo(that, e)//加入购物车
      }
    } else {
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('groupCartLists', this.data.cartList)
    wx.navigateTo({
      url: '../groupBuy/groupBuy'
    })
  },
  call: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.phone, //此号码并非真实电话号码，仅用于测试
    })
  }
})

function getCartInfo(that, e) {//加入购物车，将商品信息储存在
  var CartName = e.target.dataset.name;
  var CartPrice = e.target.dataset.price;
  var CartID = e.target.dataset.id;
  var CartSuper = e.target.dataset.storeName;
  var Cartquantity = 1;
  var totalPrices = "";
  var Cartimg = e.target.dataset.img;
  var Cartrepertory=e.target.dataset.repertory
  var carts = { productName: CartName, unitPrice: CartPrice, productId: CartID, quantity: Cartquantity, totalPrices: totalPrices, repertory: Cartrepertory, img: Cartimg}
  that.data.cartList.push(carts);
}


function getGroupDetail(that, id, agentId) {//请求后台的单个商品详情
  wx.request({
    url: app.globalData.allUrl + "/YMGW/seckillAction/getSeckillOne",
    data: {
      id: id,
      agentId: agentId
    },
    method: 'GET',
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
    //  console.log(res.data);
      that.setData({
        OtherInfo: res.data.OtherInfo,
        getGroupDetails: res.data.SeckillProduct,
        Evaluates:res.data.Evaluates
      })
      wx.setStorageSync('groupSuperName', res.data.SeckillProduct.storeName)
    }
  })

}